<?php

namespace app\common\model\weixin;



use app\common\model\BaseModel;

use common\Common;
use think\Db;

/**

 * 微信配置业务

 * @name lib_config.php

 * @package cws

 * @category model

 * @link http://www.chanekeji.com

 * @author jianfang

 * @version 2.0

 * @copyright CHANGE INC

 * @since 2016-08-05

 */

class libWeixinConfig extends BaseModel{

    private $m_config;

    

    /**

     * 构造函数

     */

    function __construct(){

        parent::__construct();

        $this->m_config = Db("weixin_config");

    }

    

    /**

	 * 根据配置列表修改

	 * @param array $configList kev=>value list

	 * @return array $result

	 */

	public function updateConfigList($configList){

		try{

			foreach($configList as  $key =>$value){

				$result = $this->m_config->where(array("item_key" => $key))->update(

							array("item_value" => $value)

						);

			}

			return Common::errorArray(0, "修改成功", true);

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}



	/**

	 * 根据配置列表修改

	 * @param array $configList kev=>value list

	 * @return array $result

	 */

	public function updateConfig($conditions,$row){

		try{

			$result = $this->m_config->where($conditions)->update($row);

			if(true == $result){

				return Common::errorArray(0, "修改成功",$result);

			}else{

				return Common::errorArray(1, "修改失败", $result);

			}

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}

	

	/**

	 * 获取所有配置用户页面配置元素提取

	 * @return array $result

	 */

	public function getConfigForGet(){

		try {

			$reslut = Db::query("SELECT * FROM weixin_config WHERE type != 'hidden' ORDER BY sort ASC");

			if($reslut){

				return Common::errorArray(0, "查询成功",$reslut);

			}else{

				return Common::errorArray(1, "查询为空", $reslut);

			}

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}

	

	/**

	 * 通过键名获取配置值

	 * @param string $key 键名

	 * @return array $result

	 */

	public function getConfigValue($key){

		try {

			$reslut = $this->m_config ->where(array('item_key'=>$key))-> find();

			if(true == $reslut){

				return Common::errorArray(0, "查询成功",$reslut['item_value']);

			}else{

				return Common::errorArray(1, "查询为空", '');

			}

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}

	

	/**

	 * 获取多个配置

	 * @param array $condition

	 * @return array $result

	 */

	public function findAllConfig($condition,$sort){

		try {

			$reslut = $this->m_config ->where($condition)->order($sort)->select();

			if(true == $reslut){

				return Common::errorArray(0, "查询成功",$reslut);

			}else{

				return Common::errorArray(1, "查询为空",$reslut);

			}

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}

	

	/**

	 * 获取对应条件的配置

	 * @param array $condition

	 * @return array $reslut

	 */

	public function findConfig($condition){

		try {

			$reslut = $this->m_config ->where($condition)-> find();

			if(true == $reslut){

				return Common::errorArray(0, "查询成功",$reslut);

			}else{

				return Common::errorArray(1, "查询为空", $reslut);

			}

		}catch(Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return Common::errorArray(1, "数据库操作失败", $ex);

		}

	}

	

	/**

	 * 添加配置

	 * @param array $row

	 * @return array $result

	 */

	public function addConfig($row){

		try {

			$keyResult = $this->m_config->where(array('item_key'=>$row['item_key']))->find();

			if($keyResult){

				return Common::errorArray(1, "key不能重复", false);

			}

			$addId = $this->m_config -> insertGetId($row);

			if(true == $addId){

				return Common::errorArray(0, "添加成功",$addId);

			}else{

				return Common::errorArray(1, "添加失败", false);

			}

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}

	

	/**

	 * 获取所有配置key=>value

	 * @return array $result

	 */

	public function findConfigKeyValue(){

		try {

			$sql = "SELECT * FROM weixin_config ORDER BY sort ASC";

			$reslut = Db::query($sql);

			foreach ($reslut as $per){

				$config[$per['item_key']] = $per['value'];

			}

			if(true == $reslut){

				return Common::errorArray(0, "查询成功",$config);

			}else{

				return Common::errorArray(1, "查询为空", $config);

			}

		}catch (Exception $ex){

		    $this->errorLog(__CLASS__, __FUNCTION__, $ex);

		    return  Common::errorArray(1, "数据库操作失败",$ex);

		}

	}

	

}